// pages/mysetting/mysetting.js
const app = getApp()
var url = require('../../utils/url.js');
var token;
var userINfo;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar:"",
    nickname:""
  },
  logout: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var token = wx.getStorageSync("token")
    wx.request({
      url: url.URl + 'logout',
      data: {},
      method: 'POST',
      header: {
        'Authorization': token
      },
      success: function (res) {
        wx.showToast({
          title: '退出成功',
          success: function () {
            wx.clearStorage()
            wx.setStorage({
              key: 'unlogin',
              data: 'unlogin',
            })
            wx.reLaunch({
              url: '/pages/index/index'
            })
          }
        })
      },fail(){
        wx.showToast({
          title: '退出成功',
          success: function () {
            wx.clearStorage()
            wx.setStorage({
              key: 'unlogin',
              data: 'unlogin',
            })
            wx.reLaunch({
              url: '/pages/index/index'
            })
          }
        })
      }
    })

  },
  editTx: function () {
    var _this = this
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        // success
        console.log(res)
        var avatar = res.tempFilePaths[0];

        console.log(avatar)
        wx.uploadFile({
          url: url.URl + 'my/profile/avatar',
          filePath: avatar,
          name: 'avatar',
          header: {
            "Content-Type": "multipart/form-data",
            'Authorization': token
          },

          success: function (res) {
            console.log(res)
            var data = JSON.parse(res.data)
            console.log(data)
            if (data.code == 200) {
              _this.setData({
                avatar: data.data
              })
              console.log(userINfo)
              userINfo.avatar = data.data
              wx.setStorage({
                key: 'userinfo',
                data: userINfo,
              })
            } else {

            }

          },
          fail: function (res) {
            wx.hideToast();
            wx.showModal({
              title: '错误提示',
              content: '上传图片失败',
              showCancel: false,
              success: function (res) { }
            })
          }
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  getnickname() {
    wx.request({
      url: url.URl + 'my/profile/nickname',
      data: {},
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        if (res.data.code == 200) {

        }
      }
    })
  },
  getphone() {
    wx.request({
      url: url.URl + 'my/profile/phone',
      data: {},
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        if (res.data.code == 200) {

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
     wx.getStorage({
       key: 'token',
       success: function(res) {
         token=res.data
         that.getphone()
       },
     })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    wx.getStorage({
      key: 'userinfo',
      success: function (res) {
        userINfo = res.data;
        that.setData({
          nickname: userINfo.nickname,
          avatar: userINfo.avatar
        })
        console.log(res.data)
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})